package com.simen.hellobytes.android;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * @author Created by SimenHi.
 * @date 创建日期 2019/1/10 09:59
 * @modify 修改者 SimenHi
 */
public class ToastL {

    private static Toast toast;

    private CharSequence msg;
    private int msgId;
    private int duration = Toast.LENGTH_SHORT;
    private Context context;
    private int gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

    private ToastL(Context context) {
        this.context = context.getApplicationContext();
    }

    public static ToastL toast(Context context, CharSequence msg) {
        return toast(context, msg, Toast.LENGTH_SHORT);
    }

    public static ToastL toast(Context context, CharSequence msg, int duration) {
        return toast(context, msg, duration, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }

    public static ToastL toastC(Context context, CharSequence msg) {
        return toastC(context, msg, Toast.LENGTH_SHORT);
    }

    public static ToastL toastC(Context context, CharSequence msg, int duration) {
        return toast(context, msg, duration, Gravity.CENTER);
    }

    public static ToastL toast(Context context, int msgId) {
        return toast(context, msgId, Toast.LENGTH_SHORT);
    }

    public static ToastL toast(Context context, int msgId, int duration) {
        return toast(context, msgId, duration, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }

    public static ToastL toastC(Context context, int msgId) {
        return toastC(context, msgId, Toast.LENGTH_SHORT);
    }

    public static ToastL toastC(Context context, int msgId, int duration) {
        return toast(context, msgId, duration, Gravity.CENTER);
    }

    public static ToastL toast(Context context, int msgId, int duration, int gravity) {
        return new ToastL(context).setMsgId(msgId).setDuration(duration).setGravity(gravity).show();
    }

    public static ToastL toast(Context context, CharSequence msg, int duration, int gravity) {
        return new ToastL(context).setMsg(msg).setDuration(duration).setGravity(gravity).show();
    }

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

    public ToastL show() {
        if (toast != null) {
            if (msg == null) {
                toast.setText(msgId);
            } else {
                toast.setText(msg);
            }
            toast.setDuration(duration);
            toast.setGravity(gravity, 0, 0);
        } else {
            toast = msg == null ? Toast.makeText(context, msgId, duration) : Toast.makeText(context, msg, duration);
            toast.setGravity(gravity, 0, 0);
        }

        toast.show();
        return this;
    }

    public CharSequence getMsg() {
        return msg;
    }

    public ToastL setMsg(CharSequence msg) {
        this.msg = msg;
        return this;
    }

    public int getDuration() {
        return duration;
    }

    public ToastL setDuration(int duration) {
        this.duration = duration;
        return this;
    }

    public Context getContext() {
        return context;
    }

    public ToastL setContext(Context context) {
        this.context = context;
        return this;
    }

    public int getGravity() {
        return gravity;
    }

    public ToastL setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public ToastL setMsgId(int msgId) {
        this.msgId = msgId;
        return this;
    }

    public int getMsgId() {
        return msgId;
    }
}
